package com.zlop.sys.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.nyvi.support.base.service.impl.BaseServiceImpl;
import com.zlop.sys.model.SysUserDO;
import com.zlop.sys.query.SysUserQuery;
import com.zlop.sys.service.SysUserService;

/**
 * SysUserServiceImpl 用户接口实现
 *
 * @author  曾令鹏
 * @since   jdk1.8
 * @version 2018年4月9日 曾令鹏
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDO> implements SysUserService {

	@Override
	@Cacheable(value="user", key = "#id"/*, condition="#id%2==0"*/)
	public SysUserDO getEntity(Long id){
		return super.getEntity(id);
	}
	
	@Override
	@Cacheable(value = "users", unless = "#result.size() <= 3") // 返回结果大于10的才缓存
	public List<SysUserDO> getList(SysUserQuery query) {
		System.out.println("------------------执行了------------------");
		return super.getList(query);
	}

	@Override
	@CachePut(value = "user", key = "#sysUser.id", condition="#sysUser.id!=null")
	public SysUserDO saveOrUpdate(SysUserDO sysUser) throws Exception {
		int ret = 0;
		if (Objects.isNull(sysUser.getId())) {
			ret = this.save(sysUser);
		}else{
			ret = this.update(sysUser);
		}
		if(ret > 0){
			return sysUser;
		}
		return null;
	}
	
	@Override
	@CacheEvict(value = "user", key = "#sysUser.id")
	public int deleteByPrimaryKey(Long id) throws Exception{
		return super.delete(id);
	}
	
	@Override
	@CacheEvict(value = "users", allEntries = true)
	public int batchDeleteByPrimaryKey(List<Long> idList) throws Exception{
		return super.batchDelete(idList);
	}

}